package com.demo.drools.util;

import com.demo.drools.velocity.VmRuleTemplate;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;

import java.io.StringWriter;
import java.util.Properties;

/**
 * @author ZhouSicheng
 * @description
 * @date 2019/3/15 14:32
 */
public final class RuleTemplateUtil {

    private static VelocityEngine engine;

    static {
        Properties properties = new Properties();
        properties.setProperty("resource.loader", "class");
        properties.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        engine = new VelocityEngine(properties);
    }

    public static String getRuleTemplateContent(VmRuleTemplate vmRuleTemplate) {
        VelocityContext context = new VelocityContext();
        context.put("packageName", vmRuleTemplate.getPackageName());
        context.put("importClasses", vmRuleTemplate.getImportClasses());
        context.put("rules", vmRuleTemplate.getRules());
        StringWriter sw = new StringWriter();
        engine.mergeTemplate("templates/template.drl.vm", "utf-8", context, sw);
        return sw.toString();
    }

}
